package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.INewsTypeDao;
import com.cn.wanxi.model.NewsTypeModel;
import com.cn.wanxi.util.JDBCUtils;
import redis.clients.jedis.Jedis;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsTypeDaoImpl implements INewsTypeDao {
    private void clearNewsTypeRedis(){
        Jedis jedis = new Jedis();
        jedis.del("news_type_id");
        jedis.del("news_type_name");
        jedis.del("news_type_href");
//        jedis.del("news_type_create_time");
//        jedis.del("news_type_update_time");
//        jedis.del("news_type_enable");
//        jedis.del("news_type_note");
    }

    private NewsTypeModel getResult(ResultSet rs) throws SQLException {
        NewsTypeModel newsType = new NewsTypeModel();
        newsType.setId(rs.getInt("id"));
        newsType.setName(rs.getString("name"));
        newsType.setHref(rs.getString("href"));
        newsType.setCreateTime(rs.getString("create_time"));
        newsType.setUpdateTime(rs.getString("update_time"));
        newsType.setEnabled(rs.getInt("enable"));
        newsType.setNote(rs.getString("note"));
        return newsType;
    }
    private NewsTypeModel getNewsTypeResult(String sql){
        NewsTypeModel newsType = null;
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()){
                newsType = getResult(rs);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return newsType;
    }
    private List<NewsTypeModel> getNewsTypeListResult(String sql){
        List<NewsTypeModel> list = new ArrayList<>();
        ResultSet rs = JDBCUtils.getResultSet(sql);
        try {
            while (rs.next()){
                NewsTypeModel newsType = getResult(rs);
                list.add(newsType);
            }
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    @Override
    public NewsTypeModel getNewsType(int id) {
        String sql = "select * from news_type where id="+id;
        return getNewsTypeResult(sql);
    }

    @Override
    public int addNewsType(NewsTypeModel newsType) {
        String sql = "insert into news_type (name,href,create_time) values('"+ newsType.getName() +
                "','"+ newsType.getHref() +"','"+ newsType.getCreateTime() +"')";
        clearNewsTypeRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public int editNewsType(NewsTypeModel newsType) {
        String sql = "update news_type set name='"+ newsType.getName() +
                "',href='"+ newsType.getHref() +"',update_time='"+ newsType.getUpdateTime() +
                "' where id="+newsType.getId();
        clearNewsTypeRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public int deleteNewsType(int id) {
        String sql = "delete from news_type where id="+id;
        clearNewsTypeRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public List<NewsTypeModel> getNewsTypeList() {
        String sql = "select * from news_type";
        return getNewsTypeListResult(sql);
    }

    @Override
    public List<NewsTypeModel> getNewsTypeList(int page, int size) {
        String sql = "select * from news_type limit "+ (page-1)*size +","+size;
        return getNewsTypeListResult(sql);
    }

    @Override
    public List<NewsTypeModel> getNewsTypeList(int page, int size, String sort) {
        String sql = "select * from news_type order by "+ sort +" limit "+ (page-1)*size +","+size;
        return getNewsTypeListResult(sql);
    }

    @Override
    public List<NewsTypeModel> searchNewsType(String sql) {
        return getNewsTypeListResult(sql);
    }

    @Override
    public long getNewsTypeCount() {
        String sql = "select count(*) from news_type";
        return JDBCUtils.getCount(sql);
    }

    @Override
    public int updateEnable(int id, int enable) {
        String sql = "update news_type set enable="+ enable +" where id="+id;
        clearNewsTypeRedis();
        return JDBCUtils.excute(sql);
    }

    @Override
    public List<NewsTypeModel> getNewsTypeListByRedis() {
        Jedis jedis = new Jedis();
        List<NewsTypeModel> list = new ArrayList<>();
        List<String> newsTypeId = jedis.lrange("news_type_id",0,-1);
        for (int i = 0;i<newsTypeId.size();i++){
            NewsTypeModel newsType = new NewsTypeModel();
            newsType.setId(Integer.valueOf(newsTypeId.get(i)));
            newsType.setName(jedis.lindex("news_type_name",i));
            newsType.setHref(jedis.lindex("news_type_href",i));
//            newsType.setCreateTime(jedis.lindex("news_type_create_time",i));
//            newsType.setUpdateTime(jedis.lindex("news_type_update_time",i));
//            newsType.setEnabled(Integer.valueOf(jedis.lindex("news_type_enable",i)));
//            newsType.setNote(jedis.lindex("news_type_note",i));
        }
        return list;
    }

    @Override
    public void setNewsTypeListInRedis(List<NewsTypeModel> list) {
        Jedis jedis = new Jedis();
        for(NewsTypeModel newsType:list){
            jedis.rpush("news_type_id", String.valueOf(newsType.getId()));
            jedis.rpush("news_type_name",newsType.getName());
            jedis.rpush("news_type_href",newsType.getHref());
//            jedis.rpush("news_type_create_time",newsType.getCreateTime());
//            jedis.rpush("news_type_update_time",newsType.getUpdateTime());
//            jedis.rpush("news_type_enable", String.valueOf(newsType.getEnabled()));
//            jedis.rpush("news_type_note",newsType.getNote());
        }
    }
}
